﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusColValueShiftBar : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { "Hidden" }; } }
        public override string[] TestStates { get { return new string[] {"IsValid", "Hidden" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return null; } }
        public override bool IsValid { get; set; }

        public event EventHandler<IFinPlusFunction> ColValueShift;
        public bool Hidden { get { return (bool)shiftsToggle.IsChecked; } set { shiftsToggle.IsChecked = value; ColShiftsHide(value); }}
        public IDictionary<string, string> ValueShiftColumns { get { return _valueShiftColumns; } }

        private IDictionary<string, string> _valueShiftColumns = new Dictionary<string, string>();

        //construct
        public FinPlusColValueShiftBar(string caption, string shifts)
        {
            InitializeComponent();

            shiftsToggle.Content = caption;
            foreach (var shift in shifts.Split(';').ToList<string>())
                AddButton(shift);

            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.ColValueShiftUp: 
                    case Commands.ColValueShiftDown:
                    case Commands.ColValShiftLevel: ColValueShifts(func); break;
                    case Commands.ColShiftsHide: ColShiftsHide(bool.Parse(func["Tag"].ToString())); break;
                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose()
        { 
        
        }

        //private
        private void AddButton(string shift)
        {
            var button = new FinPlusColValueShiftButton(shift);
            _valueShiftColumns[button.Id] = shift;
            colShiftBar.Children.Add(button);
            button.ColShift += ColShift_Click;
        }

        private void ColValueShifts(IFinPlusFunction func)
        {
            ColValueShift.Invoke(this, func);
        }

        private void ColShiftsHide(bool hidden)
        {
            if (hidden)
            {
                colShiftBar.Width = 0;
                colShiftBar.IsEnabled = false;
            }
            else
            {
                colShiftBar.Width = Double.NaN;
                colShiftBar.IsEnabled = true;
            }
        }

        //cmds
        private new enum Commands { ColValueShiftUp, ColValueShiftDown, ColValShiftLevel, ColShiftsHide }

        //events
        private void ColShift_Click(object sender, IFinPlusFunction f) { CommandRouter(f); } //ColValueShiftUp, ColValueShiftDown, ColValShiftLevel
        private void ColShiftsHide_Click(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.ColShiftsHide.ToString(), Id, User, -1, ((ToggleButton)sender).IsChecked.ToString())); }
    }
}
